/*
 * @Author: Qimu
 * @Date: 2020-04-20 13:38:40
 * @LastEditors: Qimu
 * @LastEditTime: 2020-04-23 14:09:02
 * @Description: file content
 * @FilePath: \koa2_api\controllers\lesson.js
 */
const Lesson = require('../module/schama/lesson');
const tools = require('../module/utlis/tools');
const query = require('../module/utlis/query');
const config = require('../module/config/config');

//课程业务
class LessonController {
  //课程设置(POST)
  static async setLesson(ctx) {
    ctx.status = 200;
    const {
      lesson_img, //课程图片
      lesson_name,//课程名称
      lesson_teacher, //课程老
      lesson_type,//课程类型
      lesson_office,//授课机构
      lesson_site,//授课地点
      lesson_status,//是否启用课程
    } = ctx.request.body;

    let data = {  //用户注册数据
      lesson_img,
      lesson_name,
      lesson_teacher,
      lesson_type,
      lesson_office,
      lesson_site,
      lesson_status,
    }
    let saveResult = await query.save(Lesson, data);//保存数据结果
    console.log(saveResult);
    ctx.body = {
      'data': {},
      "meta": {
        'status': 200,
        'message': '课程设置成功!'
      }
    }
  }

  //课程列表(GET)
  static async listLesson(ctx) {
    ctx.status = 200;
    let queryResult = await query.find(Lesson, {});//查找数据结果
    console.log(queryResult);
    if (queryResult.length > 0) {
      ctx.body = {
        'code': 1,
        'data': {
          'list': queryResult
        },
        'message': '课程数据返回成功!',
        'count': queryResult.length
      }
    } else {
      ctx.body = {
        'code': -1,
        'data': { 'list': [] },
        'message': '暂没课程数据!'
      }
    }
  }
}

exports = module.exports = LessonController;